<template>
  <d2-container type="card" better-scroll>
    <template slot="header">
      <div class="app-container calendar-list-container">
        <div class="filter-container">
          <el-input
            size="small"
            @keyup.enter.native="handleFilter"
            style="width: 200px;margin-right: 10px;"
            class="filter-item"
            placeholder="姓名或账户"
            v-model="listQuery.name"
          >
          </el-input>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="search"
            @click="handleFilter"
            >搜索</el-button
          >
          <el-button
            size="small"
            class="filter-item"
            v-permission:function="['teCourseclassManager_btn_add']"
            style="margin-left: 10px;"
            @click="handleCreate"
            type="primary"
            icon="edit"
            >添加</el-button
          >
        </div>
      </div>
    </template>


    <el-table
      :key="tableKey"
      :data="list"
      v-loading="false"
      size="small"
      stripe
      highlight-current-row
      style="width: 100%"
    >
      <!-- <el-table-column align="center" label="id" width="65">
      <template slot-scope="scope">
        <span>{{scope.row.id}}</span>
      </template>
    </el-table-column> -->
      <el-table-column width="200px" align="center" label="课程id">
        <template slot-scope="scope">
          <span>{{ scope.row.courseId }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="教师id">
        <template slot-scope="scope">
          <el-link @click="teainfo(scope.row.teaInfoDTO)" type="primary">{{
            scope.row.teaInfoDTO.name
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="限选人数">
        <template slot-scope="scope">
          <span>{{ scope.row.max }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="上课时间(拼接)">
        <template slot-scope="scope">
          <span>{{ scope.row.classTime }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="上课地点">
        <template slot-scope="scope">
          <span>{{ scope.row.classPlace }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column width="200px" align="center" label="0-未删除 1-已删除">
      <template slot-scope="scope">
        <span>{{scope.row.isDeleted}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="0-未确定 1-已确定">
      <template slot-scope="scope">
        <span>{{scope.row.isConfirm}}</span>
      </template>
    </el-table-column> -->
      <el-table-column width="200px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.gmtCreate | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="修改时间">
        <template slot-scope="scope">
          <span>{{ scope.row.gmtModified | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            v-permission:function="['teCourseclassManager_btn_edit']"
            size="small"
            type="success"
            @click="handleUpdate(scope.row)"
            >编辑
          </el-button>
          <el-button
            v-permission:function="['teCourseclassManager_btn_del']"
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <template slot="footer">
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </template>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="课程id" prop="courseId">
          <el-select v-model="form.courseId" placeholder="请选择">
            <el-option
              v-for="item in ke_list"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="教师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="限选人数" prop="limitNumber">
          <el-input
            v-model="form.limitNumber"
            placeholder="请输入限选人数"
          ></el-input>
        </el-form-item>
        <el-form-item label="上课时间(拼接)" >
          <el-select
            change="time_zhou"
            v-model="ke_time.star_zhou"
            placeholder="请选择开始周"
          >
            <el-option
              v-for="item in options_time.zhou"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select
            change="time_zhou"
            v-model="ke_time.end_zhou"
            placeholder="请选择结束周"
          >
            <el-option
              v-for="item in options_time.zhou"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select v-model="ke_time.xingqi" placeholder="请选择星期">
            <el-option
              v-for="item in options_time.xingqi"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

          <el-select v-model="ke_time.jie" placeholder="请选择节数">
            <el-option
              v-for="item in options_time.jie"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

  
        </el-form-item>
        <el-form-item label="上课地点" prop="classPlace">
          <el-select v-model="form.classPlace" placeholder="请选择上课地点">
            <el-option
              v-for="item in options_didain"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')" size="small">取 消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          size="small"
          type="primary"
          @click="create('form')"
          >确 定</el-button
        >
        <el-button v-else type="primary" size="small" @click="update('form')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj
} from "@/api/chooseCourse/teCourseclass/index";
import { page_1 } from "@/api/chooseCourse/teCourse/index";
import { mapGetters, mapActions, mapState } from "vuex";

export default {
  name: "teCourseclass",
  data() {
    return {
      ke_list: [],
      options: [],
      ke_time: {
        star_zhou: "",
        end_zhou: "",
        jie: "",
        xingqi: ""
      },
      options_time: {
        xingqi: [
          {
            label: "星期一",
            value: "星期一"
          },
          {
            label: "星期二",
            value: "星期二"
          },
          {
            label: "星期三",
            value: "星期三"
          },
          {
            label: "星期四",
            value: "星期四"
          },
          {
            label: "星期五",
            value: "星期五"
          },
          {
            label: "星期六",
            value: "星期六"
          },
          {
            label: "星期日",
            value: "星期日"
          }
        ],
        jie: [
          {
            label: "第一二节",
            value: "第一二节"
          },
          {
            label: "第二三节",
            value: "第二三节"
          },         
          {
            label: "第三四节",
            value: "第三四节"
          },
        
          {
            label: "第五六节",
            value: "第五六节"
          },
         
          {
            label: "第七八节",
            value: "第七八节"
          },
        
          {
            label: "第九十节",
            value: "第九十节"
          },
         
          {
            label: "一上午",
            value: "一上午"
          },
          {
            label: "一下午",
            value: "一下午"
          },
        
        ],
        zhou: [
          {
            label: "第一周",
            value: "第一周"
          },
          {
            label: "第二周",
            value: "第二周"
          },
          {
            label: "第三周",
            value: "第三周"
          },
          {
            label: "第四周",
            value: "第四周"
          },
          {
            label: "第五周",
            value: "第五周"
          },
          {
            label: "第六周",
            value: "第六周"
          },
          {
            label: "第七周",
            value: "第七周"
          },
          {
            label: "第八周",
            value: "第八周"
          },
          {
            label: "第九周",
            value: "第九周"
          },
          {
            label: "第十周",
            value: "第十周"
          },
          {
            label: "第十一周",
            value: "第十一周"
          },
          {
            label: "第十二周",
            value: "第十二周"
          },
          {
            label: "第十三周",
            value: "第十三周"
          },
          {
            label: "第十四周",
            value: "第十四周"
          },
          {
            label: "第十五周",
            value: "第十五周"
          },
          {
            label: "第十六周",
            value: "第十六周"
          },
          {
            label: "第十七周",
            value: "第十七周"
          }
        ]
      },
      options_didain: [
        {
          value: "中1202",
          label: "中1202"
        },
        {
          value: "学3302",
          label: "学3302"
        },
        {
          value: "学3302",
          label: "学1302"
        }
      ],
      form: {
        courseId: undefined,
        teacherId: undefined,
        limitNumber: undefined,
        classTime: undefined,
        classPlace: undefined,

        isConfirm: 1
      },
      rules: {
        courseId: [
          {
            required: true,
            message: "请输入外键 课程id",
            trigger: "blur"
          }
        ],
        teacherId: [
          {
            required: true,
            message: "请输入外键 教师id",
            trigger: "blur"
          }
        ],
        limitNumber: [
          {
            required: true,
            message: "请输入限选人数",
            trigger: "blur"
          }
        ],
        classTime: [
          {
            required: true,
            message: "请输入上课时间(拼接)",
            trigger: "blur"
          }
        ],
        classPlace: [
          {
            required: true,
            message: "请输入上课地点",
            trigger: "blur"
          }
        ],
        isDeleted: [
          {
            required: true,
            message: "请输入0-未删除 1-已删除",
            trigger: "blur"
          }
        ],
        isConfirm: [
          {
            required: true,
            message: "请输入0-未确定 1-已确定",
            trigger: "blur"
          }
        ],
        gmtCreate: [
          {
            required: true,
            message: "请输入创建时间",
            trigger: "blur"
          }
        ],
        gmtModified: [
          {
            required: true,
            message: "请输入修改时间",
            trigger: "blur"
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建"
      },
      tableKey: 0,
      teacher: null
    };
  },
  filters: {
    //2020-12-28T05:44:47.000+00:00转2020-12-28 05:44:47
    formatDate(time) {
      if (time) {
        return time.substring(0, 19).replace("T", " ");
      }
    },
    //文字显示30个字符
    formatText(text) {
      if (text) {
        return text.substring(0, 30) + "...";
      }
    }
  },
  created() {
    this.page_1();
    this.teacher = JSON.parse(localStorage.getItem("teacher"));
    this.getList();

    console.log(this.teacher);
    this.teacher.forEach(element => {
      console.log(element);
      this.options.push({
        value: element.teacherNumber,
        label: element.name
      });
    });
    // this.getList();
    console.log(this.teacher);
  },

  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    page_1() {
      page_1({
        page: 1,
        pageSize: 100
      }).then(res => {
        console.log(res);
        this.ke_list = res.data.list.map(item => {
          return {
            value: item.id,
            label: item.name
          };
        });
      });
    },

    ...mapActions("d2admin/adm", ["get_teacher"]),
    time_zhou(e) {
      console.log("e");
    },

    getList() {
      this.listLoading = true;

      page({
        status: 1
      }).then(response => {
        this.list = response.data;
        this.total = response.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.form = response;
        this.dialogFormVisible = true;
        this.dialogStatus = "update";
      });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delObj(row.id).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          const index = this.list.indexOf(row);
          this.list.splice(index, 1);
        });
      });
    },
    create(formName) {
      const set = this.$refs;

      set[formName].validate(valid => {
        if (valid) {
          this.form.classTime= `${this.ke_time.star_zhou}-${this.ke_time.end_zhou} ${this.ke_time.xingqi} ${this.ke_time.jie}`
        
          console.log(this.form);
          addObj(this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      const set = this.$refs;
      set[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          this.form.password = undefined;
          putObj(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    resetTemp() {}
  }
};
</script>
